package com.tmobile.books.matmadavinci;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public final class TestFragment extends Fragment implements AdOpenedListener {
	private static final String KEY_CONTENT = "TestFragment:Content";
	public static final String ARG_CHAPTER_NUMBER = "chapter_number";
	public static final String TAG = "TestFragment";
	private TextView textView;
	View disableAdNotice;
	int chapterIndex;
	AdView adViewFooter;
	private String mContent = "???";
	float fontSize;
	boolean disableAd = false; // co vo hieu hoa quang cao khong
	ScrollView scrollView;
	// handler for received Intents for the "my-event" event
	private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			// Extract data included in the Intent
			String message = intent
					.getStringExtra(Constants.BROADCAST_MESSAGE_FONT_SIZE);
			textView.setTextSize(Float.valueOf(message));
			Log.d(TAG, "Got message: " + message);
		}
	};

	public static TestFragment newInstance(String content, float s) {
		TestFragment fragment = new TestFragment();
		fragment.mContent = content;
		fragment.fontSize = s;
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		disableAd = ToastAdListener.isAdDisable(getActivity());
		if ((savedInstanceState != null)
				&& savedInstanceState.containsKey(KEY_CONTENT)) {
			mContent = savedInstanceState.getString(KEY_CONTENT);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_chapter, container,
				false);
		textView = (TextView) rootView.findViewById(R.id.content);
		scrollView = (ScrollView)rootView.findViewById(R.id.scrollView1);
		textView.setTextSize(fontSize);
		disableAdNotice = rootView.findViewById(R.id.removeAdNotice);
		adViewFooter = (AdView) rootView.findViewById(R.id.adViewFooter);
		if (disableAd) {
			adViewFooter.setVisibility(View.GONE);
			disableAdNotice.setVisibility(View.GONE);
			Log.i(TAG, "disable ad");
		} else {
			adViewFooter
					.setAdListener(new ToastAdListener(getActivity(), this));
			adViewFooter.loadAd(new AdRequest.Builder().build());
		}
		return rootView;
	}

	@Override
	public void onResume() {
		super.onResume();
		disableAd = ToastAdListener.isAdDisable(getActivity());
		if (disableAd) {
			adViewFooter.setVisibility(View.GONE);
			disableAdNotice.setVisibility(View.GONE);
			Log.i(TAG, "disable ad");
		} else {
			adViewFooter.resume();
		}		
		// Register mMessageReceiver to receive messages.
		LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
				mMessageReceiver,
				new IntentFilter(Constants.BROADCAST_RECEIVER_KEY));
		if (mContent != null) {
			// Log.d(TAG, sb);
			textView.setText(Html.fromHtml(mContent));
		}
	}

	public void changeFontSize(int size) {
		textView.setTextSize(size);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		adViewFooter.destroy();
	}

	@Override
	public void onPause() {
		super.onPause();
		// Unregister since the activity is not visible
		LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(
				mMessageReceiver);
		adViewFooter.pause();
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString(KEY_CONTENT, mContent);
	}

	@Override
	public void onAdOpened() {
		disableAdNotice.findViewById(R.id.ad_notice)
				.setVisibility(View.VISIBLE);
	}
}
